﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace AcademicPlanner.Framework.Helpers
{
	/// <summary>
	/// HtmlHelperExtensions provides static helper methods for
	/// creating lightweight html tags and controls.
	/// The methods are extensions to the ASP.NET MVC <c>HtmlHelper</c>.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public static class HtmlHelperExtensions
	{
		/// <summary>
		/// Creates a html label for a form field. 
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pFor">The html id of the field the label is for.</param>
		/// <param name="pLabel">The text of the label.</param>
		/// <param name="pRequired">Whether the field being labeled is required or not.</param>
		/// <returns>The html string for the label.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string Label(this HtmlHelper helper, string pFor, string pLabel, bool pRequired)
		{
			StringBuilder vBuilder = new StringBuilder();
			string vLabel = string.IsNullOrEmpty(pLabel) ? pFor : pLabel;
			
			vBuilder.Append("<label for=\"");
			vBuilder.Append(pFor);
			vBuilder.Append("\">");
			vBuilder.Append(vLabel);
			if (pRequired) vBuilder.Append(" <span class=\"required-marker\">*</span>");
			vBuilder.Append("</label>");

			return vBuilder.ToString();
		}

		/// <summary>
		/// Creates a html label for a form field.
		/// This method is aconvenience overload of <see cref="M:Label(HtmlHelper, string, string, bool)"/>
		/// with the fourth parameter <c>false</c>.
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pFor">The html id of the field the label is for.</param>
		/// <param name="pLabel">The text of the label.</param>
		/// <returns>The html string for the label.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string Label(this HtmlHelper helper, string pFor, string pLabel)
		{
			return Label(helper, pFor, pLabel, false);
		}

		/// <summary>
		/// Creates a html label for a form field.
		/// This method is aconvenience overload of <see cref="M:Label(HtmlHelper, string, string)"/>
		/// with the third parameter <c>null</c>.
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pFor">The html id of the field the label is for.</param>
		/// <returns>The html string for the label.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string Label(this HtmlHelper helper, string pFor)
		{
			return Label(helper, pFor, null);
		}

		/// <summary>
		/// Creates a html input of type text with size of 20 and maxlength
		/// of 100.
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pName">The name of the input field.</param>
		/// <param name="pValue">The value of the input field.</param>
		/// <returns>The html string for the input of type text.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string TextField(this HtmlHelper helper, string pName, string pValue)
		{
			return helper.TextBox(pName, pValue, 20, 100);
		}

		/// <summary>
		/// Creates a html input of type password with size of 20.
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pName">The name of the input field.</param>
		/// <param name="pValue">The value of the input field.</param>
		/// <returns>The html string for the input of type password.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string PasswordField(this HtmlHelper helper, string pName, string pValue)
		{
			return helper.Password(pName, 20, pValue);
		}

		/// <summary>
		/// Creates a html input of type password with size of 20.
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pName">The name of the input field.</param>
		/// <param name="pValue">The value of the input field.</param>
		/// <param name="pHtmlOptions"></param>
		/// <returns>The html string for the input of type password.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string PasswordField(this HtmlHelper helper, string pName, string pValue, object pHtmlOptions)
		{
			return helper.Password(pName, 20, pValue, pHtmlOptions);
		}

		/// <summary>
		/// Creates a html input of type text with size of 20 and maxlength
		/// of 100.
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pName">The name of the input field.</param>
		/// <param name="pValue">The value of the input field.</param>
		/// <param name="pHtmlOptions">An anonymous object of extra html attributes.</param>
		/// <returns>The html string for the input of type text.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string TextField(this HtmlHelper helper, string pName, string pValue, object pHtmlOptions)
		{
			return helper.TextBox(pName, pValue, 20, 100, pHtmlOptions);
		}

		/// <summary>
		/// Creates a html submit button that submits the form
		/// through an ajax request. It is assumed that a globals js
		/// object <c>gFormsCtrl</c> exists with a method <c>submit</c>
		/// that handles form submission by ajax.
		/// </summary>
		/// <param name="helper">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="pName">The name of the submit button.</param>
		/// <param name="pText">The text of the submit button.</param>
		/// <returns>The html string for the submit button.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string AjaxSubmitButton(this HtmlHelper helper, string pName, string pText)
		{
			string vFormat = "<input class=\"submit button\" type=\"submit\" name=\"{0}\" value=\" {1} \" onclick=\"return gFormsCtrl.submit(event, this.form);\" />";
			string rButton = string.Format(vFormat, pName, pText);

			return rButton;
		}

		/// <summary>
		/// Creates a html break tag for the specified number
		/// of times.
		/// </summary>
		/// <param name="htmler">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <param name="num">The number of break tags to create.</param>
		/// <returns>The html string for the break tags.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string Break(this HtmlHelper htmler, int num)
		{
			string rVal = "";

			if (num <= 0) return rVal;
			if (num > 10) num = 10;

			for (var i = 0; i < num; i++)
			{
				rVal += "<br />";
			}

			return rVal;
		}

		/// <summary>
		/// Creates a html break tag. This method is a convenience
		/// overload of <see cref="M:Break(HtmlHelper, int)"/> with
		/// the second parameter set to <c>1</c>.
		/// </summary>
		/// <param name="htmler">The ASP.NET MVC <c>HtmlHelper</c> being extended.</param>
		/// <returns>The html string for the break tag.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string Break(this HtmlHelper htmler)
		{
			return Break(htmler, 1);
		}
	}
}
